# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=soqt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")
pkgver=1.6.3
pkgrel=1
pkgdesc="A Qt Gui-toolkit binding for Coin (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/coin3d/soqt"
msys2_references=(
  'archlinux: soqt'
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-base")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-coin")
source=("https://github.com/coin3d/soqt/releases/download/v${pkgver}/soqt-${pkgver}-src.tar.gz")
sha256sums=('79342e89290783457c075fb6a60088aad4a48ea072ede06fdf01985075ef46bd')

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-qt5" && cd "${srcdir}/build-${MSYSTEM}-qt5"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DSOQT_USE_QT6=OFF \
    -DSOQT_BUILD_DOCUMENTATION=ON \
    -DSOQT_BUILD_TESTS=OFF \
    ../soqt

  ${MINGW_PREFIX}/bin/cmake --build .

  mkdir -p "${srcdir}/build-${MSYSTEM}-qt6" && cd "${srcdir}/build-${MSYSTEM}-qt6"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DSOQT_USE_QT6=ON \
    -DSOQT_BUILD_DOCUMENTATION=ON \
    -DSOQT_BUILD_TESTS=OFF \
    ../soqt

  ${MINGW_PREFIX}/bin/cmake --build .
}

package_soqt-qt5() {
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")

  cd "${srcdir}/build-${MSYSTEM}-qt5"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/soqt/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt5/LICENSE"
}

package_soqt-qt6() {
  depends+=("${MINGW_PACKAGE_PREFIX}-qt6-base")

  cd "${srcdir}/build-${MSYSTEM}-qt6"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/soqt/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt6/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
